package com.qdxwx.common.utils;

import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 *
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
public class BaJiaYunUtils {
    /*创建百家云签名*/
    public static String createSign(SortedMap<String, String> params, Map<String, Object> config) {
        StringBuilder sb = new StringBuilder();
        params.put("partner_id", TKString.trim(config.get("partnerId").toString()));
        Set<Map.Entry<String, String>> es = params.entrySet();
        for (Map.Entry<String, String> entry : es) {
            String k = entry.getKey();
            String v = entry.getValue();
            if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k).append("=").append(v).append("&");
            }
        }
        sb.append("partner_key=").append(TKString.trim(config.get("partnerKey").toString()));
        return TKConvert.toMD5(sb.toString());
    }

    /**
     * 创建百家云签名
     */
    public static String createBaiJiaYunSign(SortedMap<String, String> packageParams, String key) {
        StringBuilder sb = new StringBuilder();
        Set<Map.Entry<String, String>> es = packageParams.entrySet();

        for (Map.Entry<String, String> entry : es) {
            String k = entry.getKey();
            String v = entry.getValue();
            if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k).append("=").append(v).append("&");
            }
        }
        if (key != null && !key.equals("")) {
            sb.append("partner_key=").append(key);
        }
        return TKConvert.toMD5(sb.toString());
    }

    /*获取百家云房间url*/
    public static String getRoomUrl(HttpServletRequest request, String roomId, String type, String userType,  Map<String,Object>  baiJiaYun) throws Exception {
        SortedMap<String, String> map = new TreeMap<>();
        map.put("room_id", roomId);
        //处应在获取地址之前判断，传入map
        //User user = LoginUser.getLoginUser(request);
        //if (user==null) {
        //    return "";
        //}
        /*获取用户信息*/
        //map.put("user_number",user.getUserId()+"");
        //map.put("user_name",user.getUserName());
        if ("student".equals(userType)) {
            map.put("user_role", "0");
        } else {
            map.put("user_role", "1");
        }
        String sign = createSign(map, baiJiaYun);
        StringBuilder url = new StringBuilder();
        url.append(baiJiaYun.get("partnerDomain"));
        url.append("web/room/enter?");
        url.append(BaJiaYunUtils.mapToString(map, sign));
        if ("APP".equals(type)) {
            //如果是APP 客户端请求需要url转码 和在请求前加baijiacloud://urlpath=
            return "baijiayun://urlpath=" + TKConvert.toURLEncode(url.toString()) + "&token=token&ts=ts";
        } else {
            //否则全走WEB
            return url.toString();
        }
    }

    /*获取百家云回放url*/
    public static String getReturnUrl(String roomId, Map<String, Object> mmap) {
        SortedMap<String, String> map = new TreeMap<>();
        map.put("partner_id", mmap.get("partnerId").toString());
        map.put("room_id", roomId);
        map.put("session_id", "0");
        map.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
        String sign = createSign(map, mmap);
        map.put("sign", sign);
        return HttpUtils.doPost(mmap.get("partnerDomain") + "openapi/playback/getPlayerToken", map);
    }

    /*获取百家云点播url*/
    public static String getVideoUrl(String videoId, Map<String, Object> mmap) {
        SortedMap<String, String> map = new TreeMap<>();
        map.put("partner_id", mmap.get("partnerId").toString());
        map.put("video_id", videoId);
        map.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
        String sign = createSign(map, mmap);
        map.put("sign", sign);
        return HttpUtils.doPost(mmap.get("partnerDomain") + "openapi/video/getUrl", map);
    }


    /**
     * 删除直播间操作
     */
    public static String delLive(String roomId, Map<String, Object> mmap) {
        long createTime = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        SortedMap<String, String> message = new TreeMap<>();
        message.put("room_id", roomId);
        message.put("timestamp", createTime + "");
        message.put("sign", BaJiaYunUtils.createSign(message, mmap));
        return HttpUtils.doPost(mmap.get("partnerDomain") + "openapi/room/delete", message);
    }

    /**
     * 删除点播操作
     */
    public static String delVideo(String videoId, Map<String, Object> mmap) {
        long createTime = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        SortedMap<String, String> message = new TreeMap<>();
        message.put("video_id", videoId);
        message.put("timestamp", createTime + "");
        message.put("sign", BaJiaYunUtils.createSign(message, mmap));
        return HttpUtils.doPost(mmap.get("partnerDomain") + "openapi/video/delete", message);
    }

    /**
     * map返回string
     */
    public static String mapToString(Map<String, String> packageParams, String key) {
        StringBuilder sb = new StringBuilder();
        Set<Map.Entry<String, String>> es = packageParams.entrySet();
        for (Map.Entry<String, String> entry : es) {
            String k = entry.getKey();
            String v = entry.getValue();
            if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k).append("=").append(v).append("&");
            }
        }
        sb.append("sign=").append(key);
        return sb.toString();
    }
}
